package com.yc.dynamic.config.aspect;


import com.yc.dynamic.config.DSName;
import com.yc.dynamic.config.DynamicDataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Test1数据源切面
 */
@Aspect
@Component
public class DSTest1Aspect {

    /**
     * 切面编程默认不拦截父类方法
     * 使用+ 可以解决
     */
    @Pointcut("execution(* com.yc.*.repository.*+.find*(..))" +
            "||execution(* com.yc.*.repository.*+.get*(..))" +
            "||execution(* com.yc.*.repository.*+.exists*(..))" +
            "||execution(* com.yc.*.repository.*+.count*(..))")
    public void test1PointCut() {
    }
    /**
     * 有数据访问test1数据库时切换数据库到 test1DataSource
     * @param point
     */
    @Before("test1PointCut()")
    public void before(JoinPoint point) {
       DynamicDataSource.setDataSource(DSName.test1DataSource);
    }
    /**
     * 执行完成后切换回默认数据库 testDataSource
     * @param point
     */
    @After("test1PointCut()")
    public void adviceAfterReturn(JoinPoint point) {
        DynamicDataSource.setDataSource(DSName.testDataSource);
    }
}
